function [ctl, err] = translate_msb(msb, ctl, k)
% function to translate an msb file from Janet Fitzakerley's format
% into a control structure for MATLAB datac
% 11/19/98 P. Manis
%
err = 0;

% first, check the msb file for structure - some of the files in the directories have
% a different structure (e.g., they lack the access/compensation arguments);
% if the entry was not in the file, we create it...
s=fieldnames(msb);
if(isempty(strmatch('recseq1', s, 'exact'))) err = 1; return; end; % this is a serious problem!
if(isempty(strmatch('access', s, 'exact'))) msb.access=0; end;
if(isempty(strmatch('access1', s, 'exact'))) msb.access1 = 0; end;
if(isempty(strmatch('access2', s, 'exact'))) msb.access2 = 0; end;
if(isempty(strmatch('percomp1', s, 'exact'))) msb.percomp1 = 0; end;
if(isempty(strmatch('percomp2', s, 'exact'))) msb.percomp2 = 0; end;
if(isempty(strmatch('pnd', s, 'exact'))) msb.pnd = 0; end;
if(isempty(strmatch('div', s, 'exact'))) msb.div = 0; end;

RL = record_parse(msb.recseq1);
frec = min(RL);
lrec = max(RL);
ctl(k).seqname=make_sequence(msb.fname1, frec);
% check filename for an extension - if not one, add it
[path file ext ver] = fileparts(msb.fname1);
if(isempty(ext)) msb.fname1 = strcat(msb.fname1, '.jlf'); end;
ctl(k).filename = msb.fname1;
ctl(k).path = strcat('H:\CNCULT\', sprintf('%5s\\', upper(msb.fname1(3:7))));
ctl(k).quality = 'good';
ctl(k).mode = 'VC';
ctl(k).protocol = msb.protocol;
ctl(k).solution = msb.legend1;
ctl(k).pnd = number_arg(msb.pnd);
ctl(k).div = number_arg(msb.div);
ctl(k).sequence = '';
ctl(k).recbeg = frec;
ctl(k).recend = lrec;
ctl(k).skip = 1;
ctl(k).groupavg = 1;
ctl(k).vgain = 2;
ctl(k).igain = -number_arg(msb.gain1);
ctl(k).wgain = 1.;
ctl(k).tbeg = 0;
ctl(k).tend = 200; 
ctl(k).thresh = 0;
ctl(k).jpot = -8;
ctl(k).access = number_arg(msb.access1);
ctl(k).comp = number_arg(msb.percomp1)/100;
ctl(k).alignwin = 0;
ctl(k).leakwin = 2;
ctl(k).transwin = 2;
if(strcmp(ctl(k).protocol, 'iva')) ctl(k).leakwin = 3; end;
ctl(k).transwdur = 45;
ctl(k).deadwin = 0.1;
ctl(k).durho = 10;
ctl(k).durs1 = 50;
ctl(k).durs2 = 100;
ctl(k).durs3 = 0;
ctl(k).diff = 0;
ctl(k).file = '';
ctl(k).altmode = 0;
ctl(k).bactn = 1;
ctl(k).cycle = 1000;
ctl(k).bstart = 0;
ctl(k).bend = 2;
ctl(k).vlmax = -60.0;
ctl(k).vlmin = -120.0;
ctl(k).leakt = 11;
ctl(k).Gin = 0.0;
ctl(k).Rin = 0.0;
ctl(k).iHold = 0.0;
ctl(k).Vm0 = 0.0;
ctl(k).Rmp = 0.0;
ch = clock;
ctl(k).entrydate = strcat(date, sprintf(' %02.0f:%02.0f:%02.0f', ch(4), ch(5), ch(6)));
ctl(k).lastchanged = strcat(date, sprintf(' %02.0f:%02.0f:%02.0f', ch(4), ch(5), ch(6)));
if(strcmp(msb.analtype, 'dif'))
   ctl(k).subtract = 'sub';
   submode = 1;
else
   ctl(k).subtract = 'no';
   submode = 0;
end
if(submode)
% check filename for an extension - if not one, add it
[path file ext ver] = fileparts(msb.fname2);
if(isempty(ext)) msb.fname2 = strcat(msb.fname2, '.jlf'); end;
   ctl(k).linkfile = msb.fname2;
   [RL2, err] = record_parse(msb.recseq2);
   ctl(k).sub_frec = min(RL2);
   ctl(k).sub_lrec = max(RL2);
   ctl(k).sub_access = number_arg(msb.access2);
   ctl(k).sub_comp = number_arg(msb.percomp2)/100;
   ctl(k).sub_igain = number_arg(msb.gain2);
else
   ctl(k).linkfile = '';
   ctl(k).sub_frec = '0';
   ctl(k).sub_lrec = '0';
   ctl(k).sub_access = '0';
   ctl(k).sub_comp = '0';
   ctl(k).sub_igain = '0';
end

